on: pull_request

name: preview-pr
permissions:
  pull-requests: write

env:
  flutter_version: "3.10.x"

jobs:
  main:
    name: preview pr web build
    runs-on: ubuntu-latest
    steps:
      - name: checkout code
        uses: actions/checkout@v3
      - name: set up flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.flutter_version }}
          channel: "stable"
      - name: flutter version
        run: flutter --version
      - name: flutter pub get
        run: flutter pub get
      - name: flutter build web
        run: flutter build web --no-tree-shake-icons --base-href "/pr-preview/${{ github.event.pull_request.number }}/"
      - name: upload to s3
        id: s3
        uses: shallwefootball/s3-upload-action@master
        with:
          aws_key_id: ${{ secrets.S3_ACCESS_KEY }}
          aws_secret_access_key: ${{ secrets.S3_SECRET_KEY}}
          aws_bucket: ${{ secrets.S3_BUCKET }}
          endpoint: ${{ vars.S3_ENDPOINT }}
          source_dir: build/web
          destination_dir: pr-preview/${{ github.event.pull_request.number }}
      - name: find first comment
        uses: peter-evans/find-comment@v2
        id: fc
        with:
          issue-number: ${{ github.event.pull_request.number }}
          comment-author: "github-actions[bot]"
          body-includes: preview
      - name: create or update comment
        uses: peter-evans/create-or-update-comment@v3
        with:
          comment-id: ${{ steps.fc.outputs.comment-id }}
          issue-number: ${{ github.event.pull_request.number }}
          body: |
            👋 Hi, I have prepared a preview of this PR with web build: 
            [preview \#${{ github.event.pull_request.number }}](${{ vars.S3_DOMAIN }}/pr-preview/${{ github.event.pull_request.number }})
          edit-mode: replace
